// pages/02admin/13sellingGoods/sellingGoods.js
var app = getApp()
var appData = app.globalData
var keyword, pageNo
Page({
   data: {
      isShowInputLabelArr: [0, 0],
      goodsList: [],
      edit_id: -1,
      pick_i: 0,
      exchange_id: -1,
      mode: ''
   },

   sorting: function () {
      this.setData({ mode: this.data.mode == '' ? 'sort' : '' })
   },

   onLoad: function (opt) {
      var e = { detail: { value: '' } }
      this.getGoodsList(e)
      app.setNoticetGoodsList(function (res) {
         t.updateGoodsList(res)
      })
   },

   getGoodsList: function (e) {
      var t = this
      if (keyword != e.detail.value) {
         keyword = e.detail.value
         pageNo = 1
         t.data.goodsList = []
      }
      var param = {
         uid: appData.shopInfo.Biz_ID,
         pageno: pageNo,
         keyword: keyword
      }
      app.getJsonData('api/space/product_list.php', param, function (res) {
         if (res.code == 0) {
            t.setData({ goodsList: t.data.goodsList.concat(res.data) })
            pageNo++//获取成功后，页码增加
         } else {
            app.toast(res.message)
         }
      })
   },
   //排序
   toExchange: function (e) {
      var td = this.data
      var t = this
      var index = e.currentTarget.id
      if (td.exchange_id != -1 && index != '') {
         var param = {
            id1: td.goodsList[index].id,
            id2: td.goodsList[td.exchange_id].id,
            shop_id: appData.shopInfo.Biz_ID
         }
         //tp5
         app.getNetData('product/mg/exchangeSort', param, function (res) {
            console.log('试试看', res)
            t.exchange(index, td.exchange_id)
            t.setData({
               tableCateList: res.data,
               exchange_id: -1
            })
         })
      } else {
         this.setData({ exchange_id: index == '' ? -1 : index })
      }
      console.log('【【【【【td===', td)
      console.log('【【【【【e ===', e)
   },

   exchange: function (i, j) {
      var p = this.data.goodsList[i]
      this.data.goodsList[i] = this.data.goodsList[j]
      this.data.goodsList[j] = p
      this.setData({ goodsList: this.data.goodsList })
   },

   updateGoodsList: function (r) {
      var list = this.data.goodsList
      for (var i = 0; i < list.length; i++) {
         if (r.id == list[i].id) {
            list[i] = r
            break
         }
      }
      this.setData({ goodsList: list })
   },

   editGoods: function (e) {
      if (app.globalData.loginMode != 'boss') {
         app.toast('需要掌柜登录才能操作')
         return
      }

      let i = e.currentTarget.id
      var url_end = ''
      if (i != '') {
         let oneGoods = JSON.stringify(this.data.goodsList[i])
         url_end = '?goods=' + oneGoods
      }

      wx.navigateTo({
         url: '/pages/02admin/12goodsShelves/goodsShelves' + url_end
      })
   },

   delConfirm: function (e) {
      var t = this
      app.confirm('重要提示', '确认删除吗？', '删除', function (res) {
         console.log('delConfirm: function', res)
         if (res) {
            t.delGoods(e)
         }
      })
   },

   delGoods: function (e) {
      if (app.globalData.loginMode != 'boss') {
         app.toast('需要掌柜登录才能操作')
         return
      }

      let i = e.currentTarget.id
      var list = this.data.goodsList
      var param = { id: list[i].id }
      var that = this
      app.getNetData('product/mg/delete', param, function (res) {
         list.splice(i, 1)
         that.setData({ goodsList: list })
      })
   },

   onReachBottom: function () {
      var e = { detail: { value: keyword } }
      this.getGoodsList(e)
   },

   onUnload: function () {
      pageNo = 1//要恢复，不然再进来时，会是上次的值
   },
})